﻿using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Myotragus.Data.Containers
{
	public class TypeNameParser : ITypeNameParser
	{
		private static readonly Regex SplitExpression =
				new Regex(@"^(?<TypeName>\w+(\.\w+)*)(\{(?<GenericArg1>\w+(\.\w+)*)(?<GenericArgI>\,\w+(\.\w+)*)*\})?$");

		public IEnumerable<string> Parse(string typeName)
		{
			typeName = Regex.Replace(typeName, "\\s", "");
			var match = SplitExpression.Match(typeName);

			yield return match.Groups["TypeName"].Value;

			if (!match.Groups["GenericArg1"].Success) yield break;
			yield return match.Groups["GenericArg1"].Value;

			foreach (var arg in match.Groups["GenericArgI"].Captures.Cast<Capture>()
		.Select(c => c.Value.Substring(1)))
				yield return arg;
		}
	}
}